<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class msoft extends CI_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "soft";
    public $primaryKey = "SOFT_ID";
    public $resCode = "SOFT";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
		$fieldData['MAN_ID'] = 0;
		$fieldData['LICE_ID'] = 0;
		$fieldData['CAT_ID'] = 0;
		$fieldData['USE_ID'] = 0;
		$fieldData['SOFT_NAME'] = '';
		$fieldData['SOFT_NAME_DESCRIPTION'] = '';
		$fieldData['SOFT_CONTENT'] = '';
		$fieldData['SOFT_FILESIZE'] = '';
		$fieldData['SOFT_CONTENT_SHORT'] = '';
		$fieldData['SOFT_TIME_CREATE'] = time();
		$fieldData['SOFT_TIME_UPDATE'] = time();
		$fieldData['SOFT_STATUS'] = 1;
		$fieldData['SOFT_VIEW'] = 0;
		$fieldData['SOFT_THUMB'] = '';
		$fieldData['SOFT_CODE'] = '';
		$fieldData['SOFT_DOWN'] = 0;
		$fieldData['SOFT_TIME_USE_LIMIT'] = 0;
		$fieldData['SOFT_REQUIREMENT_OS'] = '';
		$fieldData['SEO_H1'] = '';
		$fieldData['SEO_KEYWORD'] = '';
		$fieldData['SEO_DESCRIPTION'] = '';
		$fieldData['SEO_METATITLE'] = '';
		$fieldData['SOFT_VERSION'] = '';
		$fieldData['SOFT_ICON'] = '';
		$fieldData['SOFT_CACHE_MAN'] = null;
		$fieldData['SOFT_CACHE_CATEGORY'] = null;
		$fieldData['SOFT_CACHE_LICENSE'] = null;
		$fieldData['SOFT_CACHE_IMAGES'] = null;
		return $fieldData;
    }

    function get($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->first_row();
    }

    function gets($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		
        $this->db->from($this->mytable);
        return $this->db->get()->result();
    }

    function where_in($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        $this->db->from($this->mytable);
        if ($val != '')
            $this->db->where_in($this->mytable . "." . $param, $val);
        return $this->db->get()->result();
    }

    function num($val = '', $param = null) {
       if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		
        $this->db->from($this->mytable);
        return $this->db->count_all_results();
    }

    function update($val = '', $param = null, $arr_update) {
        if ($param == null)
            $param = $this->primaryKey;

        if ($val != '')
            $this->db->where($param, $val);
		$arr_update['SOFT_TIME_UPDATE'] = time();
        $arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
    }

    function insert($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field());
        $this->db->insert($this->mytable, $arr_insert);
        return $this->db->insert_id();
    }

    function delete($val = '', $param = null) {
        if ($val != '') {
            $this->db->where($param, $val);
            $this->db->delete($this->mytable);
        }
    }
	function getsSoft($val = '', $param = null, $type='gets') {
		if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable." as p");
		//$this->db->join("category as s", "p.CAT_ID = s.CAT_ID");
		//$this->db->join("license as s1", "p.LICE_ID = s1.LICE_ID");
		//$this->db->join("manufacturer as s2", "p.MAN_ID = s2.MAN_ID");
		
		//return count all result
		if($type =='num')
			return $this->db->count_all_results();
		
		$softs = $this->db->get()->result_array();
		return $this->merge_softs($softs);
	
	}
	
	function getSoft($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		$this->db->select(" s.*,s1.*,s2.*,p.*");
        $this->db->from($this->mytable." as p");
		$this->db->join("category as s", "p.CAT_ID = s.CAT_ID");
		$this->db->join("license as s1", "p.LICE_ID = s1.LICE_ID");
		$this->db->join("manufacturer as s2", "p.MAN_ID = s2.MAN_ID");
        return $this->db->get()->first_row();
    }

	function getsNewUpdate($cateID, $limit = 10){
		$this->db->limit($limit);
		$this->db->order_by('SOFT_TIME_UPDATE desc');
		$this->db->where("p.SOFT_STATUS",1);
		if(is_array($cateID)){
			 $this->db->where_in('p.CAT_ID', $cateID);
		}
		else if($cateID >0) {
		$this->db->where("p.CAT_ID",$cateID);
		}
        $this->db->from($this->mytable." as p");
		$softs = $this->db->get()->result_array();
		return $this->merge_softs($softs);
	}
	
	function getsNewInsert($cateID, $limit = 10){
		$this->db->limit($limit);
		$this->db->order_by('SOFT_ID desc');
		$this->db->where("p.SOFT_STATUS",1);
		if(is_array($cateID)){
			 $this->db->where_in('p.CAT_ID', $cateID);
		}
		else if($cateID >0) {
		$this->db->where("p.CAT_ID",$cateID);
		}
        $this->db->from($this->mytable." as p");
		$softs = $this->db->get()->result_array();
        return $this->merge_softs($softs);
	}
	
	/*
		Lấy danh sách các chương trình mới nhất theo Man ID
	*/
	function getsNewMan($manID = 0, $orderBy = ''){
		$contidion = array();
		$contidion['SOFT_STATUS'] = 1;
		if(is_array($manID)){
			 $this->db->where_in('p.MAN_ID', $manID);
		}
		else
		{
		if($manID >0)
			$contidion['p.MAN_ID'] = $manID;
		}
		
		if($orderBy =='popular'){
			$this->db->order_by('SOFT_DOWN desc');
		}
		else {
			$this->db->order_by('SOFT_TIME_CREATE desc');
		}
		return $this->getsSoft($contidion);
	}
	
	function getsSearch($keyword = '',$manCode = '', $orderBy = '', $type='gets'){
		$contidion = array();
		$contidion['SOFT_STATUS'] = 1;
		if($manCode !='' &&$manCode>0 ){
			$contidion['MAN_ID'] = $manCode;
		}
		$this->db->where("(`SOFT_NAME` LIKE '%".$keyword."%' OR `SOFT_NAME_DESCRIPTION` LIKE '%".$keyword."%' OR t.TAG_NAME = '".$keyword."')"); 
		if($orderBy =='popular'){
			$this->db->order_by('SOFT_DOWN desc');
		}
		else {
			$this->db->order_by('SOFT_TIME_CREATE desc');
		}
		$this->db->join("soft_tag as st","p.SOFT_ID = st.SOFT_ID");
		$this->db->join("tag as t","t.TAG_ID = st.TAG_ID");
		if($type == 'gets'){
			$this->db->group_by("p.SOFT_ID");
			return $this->getsSoft($contidion,null,'gets');
		}
		else {
		$this->db->select ('COUNT(DISTINCT p.soft_id) AS `numrows`');
		$this->db->where($contidion);
		$this->db->from($this->mytable." as p");
		$num= $this->db->get()->first_row();
		return $num->numrows;

		}
		/*
		SELECT COUNT(DISTINCT p.soft_id) AS `numrows` FROM (`soft` as p) 
JOIN `soft_tag` as st ON `p`.`SOFT_ID` = `st`.`SOFT_ID` JOIN `tag` as t ON `t`.`TAG_ID` = `st`.`TAG_ID` WHERE 
(`SOFT_NAME` LIKE '%h%' OR `SOFT_NAME_DESCRIPTION` LIKE '%h%' OR t.TAG_NAME = 'h') 
AND `SOFT_STATUS` = 1 


ORDER BY `SOFT_TIME_CREATE` desc
*/
		
	}
	
	function getRelated($cateID = 0, $softID = 0, $limit = 10, $type = '1'){
		$this->db->select("SOFT_ID,USE_ID,SOFT_NAME,SOFT_NAME_DESCRIPTION,SOFT_FILESIZE,SOFT_CODE,SOFT_THUMB,SOFT_ICON,SOFT_VERSION,SOFT_CACHE_MAN,SOFT_CACHE_CATEGORY,SOFT_CACHE_LICENSE,SOFT_CACHE_IMAGES,SOFT_DOWN");
		$this->db->limit($limit);
		$this->db->order_by("SOFT_ID desc");
		if($type == '1'){
			return $this->getsSoft(array(
				'SOFT_ID <'=>$softID,
				'CAT_ID'=>$cateID,
			));
		}
		else {
			return $this->getsSoft(array(
				'SOFT_ID >'=>$softID,
				'CAT_ID'=>$cateID,
			));
		}
	}
	
	function updateView($softID){
		//update view
        $this->db->where("SOFT_ID", $softID);
        $this->db->set('SOFT_VIEW', 'SOFT_VIEW+1', false);
        $this->db->update($this->mytable, array());
	}
	
	function updateDownload($softID){
		//update view
        $this->db->where("SOFT_ID", $softID);
        $this->db->set('SOFT_DOWN', 'SOFT_DOWN+1', false);
        $this->db->update($this->mytable, array());
	}
	
	function merge_softs($softs){
		for($i=0; $i<count($softs); $i++){
			$cache = json_decode($softs[$i]['SOFT_CACHE_MAN'],true);
			$softs[$i] = array_merge($cache, $softs[$i]);
			$cache = json_decode($softs[$i]['SOFT_CACHE_CATEGORY'],true);
			$softs[$i] = array_merge($cache, $softs[$i]);
			$cache = json_decode($softs[$i]['SOFT_CACHE_LICENSE'],true);
			$softs[$i] = (object)array_merge($cache, $softs[$i]);
		}
		return (object)$softs;
	}
	
	function getImageDetail($softID){
		$this->db->where("SOFT_ID",$softID);
		$this->db->from("soft_images");
		return $this->db->get()->result();
	}
	
	function cache_man($manID = 0){
		$this->load->model("mmanufacturer");
		$man = $this->mmanufacturer->get($manID, 'MAN_ID');
		return json_encode($man);
	}
	
	function cache_cate($cateID = 0){
		$this->load->model("mcategory");
		$cat = $this->mcategory->get($cateID, 'CAT_ID');
		return json_encode($cat);
	}
	function cache_lic($licID = 0){
		$this->load->model("mlicense");
		$lic = $this->mlicense->get($licID, 'LICE_ID');
		return json_encode($lic);
	}
	function cache_images($softID = 0){
		$this->load->model("msoft_images");
		$images = $this->msoft_images->gets($softID, 'SOFT_ID');
		return json_encode($images);
	}
	
	function update_cache_man($manID){
		$arr_update = array();
		$arr_update['SOFT_CACHE_MAN'] = $this->cache_man($manID);
		$this->db->where("MAN_ID", $manID);
		$this->db->from($this->mytable);
		$arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
	}
	
	function update_cache_lic($licID){
		$arr_update = array();
		$arr_update['SOFT_CACHE_LICENSE'] = $this->cache_lic($licID);
		$this->db->where("LICE_ID", $licID);
		$this->db->from($this->mytable);
		$arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
	}
	
	function update_cache_cate($cateID){
		$arr_update = array();
		$arr_update['SOFT_CACHE_CATEGORY'] = $this->cache_cate($cateID);
		$this->db->where("CAT_ID", $cateID);
		$this->db->from($this->mytable);
		$arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
	}
	
	function numLike($val = '', $param = null) {
       if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->like($val);
        else if ($val != '')
            $this->db->like($param, $val);
		
        $this->db->from($this->mytable);
        return $this->db->count_all_results();
    }
	
	function getsLike($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->like($val);
        else if ($val != '')
            $this->db->like($param, $val);
		
        $this->db->from($this->mytable);
		$softs = $this->db->get()->result_array();
		return $this->merge_softs($softs);
    }
	
	function getCategory($cat_id = 1){
		$this->load->model("mcategory");
		$this->load->model("scategory");
		$arr_catemobile = array(249,250,251,252,253);
		$this->db->order_by("CAT_ORDER desc");
		$this->db->where_not_in('CAT_ID',$arr_catemobile);
		$data['categoryParent'] = $this->mcategory->gets(array('CAT_PARENT'=>0));

		if ($data['categoryParent']) {
			for($i=0; $i<count($data['categoryParent']); $i++) {
				$cate = $data['categoryParent'][$i];
				if($cate->CAT_ID == 3){
					foreach($arr_catemobile as $r){
						$data['categoryParent'][$i]->CHILD[] =  $this->scategory->get_cate_info($r);
					}
				}
				else {
				$data['categoryParent'][$i]->CHILD =  $this->scategory->get_cat_format_tree($data['categoryParent'][$i]->CAT_ID);

				}
			}
		}

		$data['categorySub'] = $this->scategory->get_cat_format_tree($cat_id);
		if($data['categorySub']){
			for ($i=0; $i<count($data['categorySub']); $i++){
				 $sub = $this->scategory->get_cat_format_tree($data['categorySub'][$i]['CAT_ID']);
				 $data['categorySub'][$i]['SUB_DATA'] = array();
				 if($sub){
					$data['categorySub'][$i]['SUB_DATA'] = $sub;
				 }
			}
		}
		return $data;
		}

}

?>